// a Genetic Insertion Sort
// By Ben 02/10/2018

#include <iostream>
using namespace std;

template<typename T>
void InsertionSort(T* Items, int Size){
	int j = 0;
	int i = 0;
	T key;

	for (i = 0; i < Size; i++){
		key = Items[i];
		j = (i - 1);

		while (j >= 0 && Items[j] > key){
			Items[j + 1] = Items[j];
			j--;
		}
		Items[j + 1] = key;
	}
}

void Display(int *Nums, int Size){
	for (int i = 0; i < Size; i++){
		if (i < Size-1){
			std::cout << Nums[i] << ", ";
		}
		else{
			std::cout << Nums[i];
		}
	}
	std::cout << endl;
}

int main(){

	int nums[] = { 5, 7, 2, 6, 4, 1, 3, 10, 40, 12, 13, 16, 20, 15, 17 };
	int n_size = sizeof(nums) / sizeof(int);

	//Output un-sorted
	std::cout << "Unsorted : ";
	Display(nums, n_size);

	//Sort array
	InsertionSort<int>(nums, n_size);
	//Output sorted
	std::cout << "Sorted : ";
	Display(nums, n_size);

	system("pause");
	return 0;
}